package com.morristaedt.mirror.configuration;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.support.annotation.NonNull;

import com.morristaedt.mirror.BuildConfig;
import com.morristaedt.mirror.modules.voice.TtsModule;
import com.morristaedt.mirror.requests.ForecastRequest;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created by HannahMitt on 9/26/15.
 */
public class ConfigurationSettings {

    /**
     * Hardcode on to enable features outside of their regularly scheduled hours
     */
    private static final boolean DEMO_MODE = false;

    private static final String PREFS_MIRROR = "MirrorPrefs";
    public static final String KEY_WEATHER = "weather";

    private static final String FORECAST_UNITS = "forecast_units";
    private static final String TEXT_COLOR = "text_color";
    private static final String USE_VOICE_DETECTION = "voice_detection";
    private static final String SHOW_CALENDAR = "show_calendar";
    private static final String USE_FACE_DETECTION = "face_detection";
    private static final String INVERT_XKCD = "invert_xkcd";
    private static final String LAT = "lat";
    private static final String LON = "lon";
    private static final String SHOW_COUNTDOWN = "show_countdown";
    private static final String COUNTDOWN_END = "countdown_end";
    private static final String WEATHER_RAW = "weather_RAW";
    private static final String WEATHER_DAY = "weather_day";
    private static final String WEATHER_LATEST_DATE = "weather_latest_day";
    private static final String TTS_VOICE = "tts_voice";

    @NonNull
    private SharedPreferences mSharedPrefs;

    private String mForecastUnits;

    private boolean mShowVoiceDetection;
    private boolean mShowNextCalendarEvent;
    private boolean mUseFaceDetection;
    private boolean mInvertXKCD;
    private boolean mShowCountdown;

    private long mCountdownEnd;

    private String mLatitude;
    private String mLongitude;
    private int mTtsVoiceIndex;


    private int mTextColor;
    private String presKey;

    private static long MAX_SYNC_WEATHER_TIME = 2*60*3600;
    private DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());

    public ConfigurationSettings(Context context) {
        mSharedPrefs = context.getSharedPreferences(PREFS_MIRROR, Context.MODE_PRIVATE);
        readPrefs();
    }


    public ConfigurationSettings(Context context,String key) {
        this.presKey = key;
        mSharedPrefs = context.getSharedPreferences(key, Context.MODE_PRIVATE);
    }

    private void readPrefs() {
        mForecastUnits = mSharedPrefs.getString(FORECAST_UNITS, ForecastRequest.UNITS_US);
        mTextColor = mSharedPrefs.getInt(TEXT_COLOR, Color.WHITE);
        mShowVoiceDetection = mSharedPrefs.getBoolean(USE_VOICE_DETECTION, false);
        mShowNextCalendarEvent = mSharedPrefs.getBoolean(SHOW_CALENDAR, false);
        mUseFaceDetection = mSharedPrefs.getBoolean(USE_FACE_DETECTION, false);
        mInvertXKCD = mSharedPrefs.getBoolean(INVERT_XKCD, false);
        mShowCountdown = mSharedPrefs.getBoolean(SHOW_COUNTDOWN, false);
        mCountdownEnd = mSharedPrefs.getLong(COUNTDOWN_END, System.currentTimeMillis());

        mLatitude = mSharedPrefs.getString(LAT, "");
        mLongitude = mSharedPrefs.getString(LON, "");
        mTtsVoiceIndex = mSharedPrefs.getInt(TTS_VOICE, TtsModule.voicerCloudIndex);

    }

    public void setIsCelsius(boolean isCelsius) {
        SharedPreferences.Editor editor = mSharedPrefs.edit();
        editor.putString(FORECAST_UNITS, isCelsius ? ForecastRequest.UNITS_SI : ForecastRequest.UNITS_US);
        editor.apply();
    }

    public void setTextColorRed(int red){
        setTextColor(Color.rgb(red, Color.green(mTextColor), Color.blue(mTextColor)));
    }

    public void setTextColorGreen(int green){
        setTextColor(Color.rgb(Color.red(mTextColor), green, Color.blue(mTextColor)));
    }

    public void setTextColorBlue(int blue){
        setTextColor(Color.rgb(Color.red(mTextColor), Color.green(mTextColor), blue));
    }

    public void setTextColor(int color){
        mTextColor = color;
        SharedPreferences.Editor editor = mSharedPrefs.edit();
        editor.putInt(TEXT_COLOR, color);
        editor.apply();
    }


    public void setShowVoiceDetection(boolean show) {
        mShowVoiceDetection = show;
        SharedPreferences.Editor editor = mSharedPrefs.edit();
        editor.putBoolean(USE_VOICE_DETECTION, show);
        editor.apply();
    }

    public void setFaceDetection(boolean show) {
        mUseFaceDetection = show;
        SharedPreferences.Editor editor = mSharedPrefs.edit();
        editor.putBoolean(USE_FACE_DETECTION, show);
        editor.apply();
    }

    public void setLatLon(String latitude, String longitude) {
        mLatitude = latitude.trim();
        mLongitude = longitude.trim();

        SharedPreferences.Editor editor = mSharedPrefs.edit();
        editor.putString(LAT, mLatitude);
        editor.putString(LON, mLongitude);
        editor.apply();
    }


    public void setShowCountdown(boolean showCountdown){
        mShowCountdown = showCountdown;
        SharedPreferences.Editor editor = mSharedPrefs.edit();
        editor.putBoolean(SHOW_COUNTDOWN, showCountdown);
        editor.apply();
    }

    public void setCountdownTime(int days, int hours, int mins, int secs){
        mCountdownEnd = System.currentTimeMillis();
        mCountdownEnd += (((days*24l+hours)*60l+mins)*60l+secs)*1000l;
        SharedPreferences.Editor editor = mSharedPrefs.edit();
        editor.putLong(COUNTDOWN_END, mCountdownEnd);
        editor.apply();
    }



    public boolean getIsCelsius() {
        return ForecastRequest.UNITS_SI.equals(mForecastUnits);
    }

    public String getForecastUnits() {
        return mForecastUnits;
    }

    public int getTextColor() {
        return mTextColor;
    }


    public boolean showVoiceDetection() {
        return mShowVoiceDetection;
    }

    public boolean showNextCalendarEvent() {
        return mShowNextCalendarEvent;
    }


    public boolean showFaceDetection() {
        return mUseFaceDetection;
    }

    public boolean invertXKCD() {
        return mInvertXKCD;
    }

    public boolean showCountdown(){
        return mShowCountdown;
    }

    public Date getCountdownEnd(){
        return new Date(mCountdownEnd);
    }


    public void putTtsVoice(int ttsVoiceIndex){
        SharedPreferences.Editor editor = mSharedPrefs.edit();
        editor.putInt(TTS_VOICE, ttsVoiceIndex);
        editor.apply();
    }

    public int getTtsVoice(){
        return mTtsVoiceIndex;
    }

    public void putWeatherRaw(String raw,Date date){
        SharedPreferences.Editor editor = mSharedPrefs.edit();
        editor.putString(WEATHER_RAW, raw);
        editor.putString(WEATHER_DAY,formatter.format(date));
        editor.putLong(WEATHER_LATEST_DATE,date.getTime());
        editor.apply();
    }

    public String getWeatherRaw(){
       return mSharedPrefs.getString(WEATHER_RAW, "");
    }

    public String getWeatherDay(){
        return mSharedPrefs.getString(WEATHER_DAY, "");
    }

    public long getWeatherLatestDate(){
        return mSharedPrefs.getLong(WEATHER_LATEST_DATE, new Date().getTime());
    }

    /**
     * 是否需要同步天气数据
     * 已经不是今天 或者 过了2小时就需要更新
     */
    public boolean isNeed2SyncWeather(Date date){
        return date != null && (!getWeatherDay().equals(formatter.format(date)) || date.getTime() - getWeatherLatestDate() >= MAX_SYNC_WEATHER_TIME) ;
    }


    public static boolean isDebugBuild() {
        return BuildConfig.DEBUG;
    }

    /**
     * Whether we're ignoring timing rules for features
     *
     * @return
     */
    public static boolean isDemoMode() {
        return DEMO_MODE;
    }

}
